import * as api from "../until/getpro";

export default {
  namespace: "info",
  state: {
	str:"",
	token:"",
	list:[],
	count:0,
	userName:"",
	nickName:"",
	password:"",
	avatar:"",
	id:"",
	pages:"",
	count:"",
	current:"",
	productlist:"",
	page:"",
	totalCount:"",
	mess:"",
	productName:"",
	productDesc:"",
	productPrice:"",
	productQuantity:"",
	productIamge:"",
	productCategory:"",
	productOnSale:"",
	productCreatedAt:"",
	orderNo:"",
	orderPrice:"",
	userId:"",
	orderCreatedAt:"",
	orderIsPayed:"",
  },
  reducers: {
    change(state, { payload }) {
      return { ...state, ...payload };
    },
  },
  effects:{
  	*getData(payload,{call,put}){
  		console.log(payload)
  		const result=yield call(api.getLogin,payload.payload)
  		console.log(result.data.code)
  		yield put({
  			type:'change',
  			payload:{
  				str:result.data.code,
				token:result.data.token
  			}
  		})
  	},
	*getuserData(payload,{call,put}){
		console.log(payload)
		const result=yield call(api.getuserList,payload.payload)
		console.log(result)
		yield put({
			type:'change',
			payload:{
				list:result.data.users,
				pages:result.data.pages,
				count:result.data.totalCount,
				current:payload.payload.page
			}
		})
	},
	*addUser(payload,{call,put}){
		console.log(payload)
		const result=yield call(api.addUser,payload.payload)
		console.log(result)
		yield put({
			type:'change',
			payload:{
				mess:result.data.code
			}
		})
	},
	*deleteUser(payload,{call,put}){
		console.log(payload)
		const result=yield call(api.deleteUser,payload.payload)
		console.log(result)
		
	},
	*editUser(payload,{call,put}){
		console.log(payload)
		let a=function(){
			return api.editUser(payload.payload)
		}  
		const result=yield call(a,payload.payload)
		console.log(result)
	},
	*getUserInfo(payload,{call,put}){
		console.log(payload)
		const result=yield call(api.getUserInfo,payload.payload)
		console.log(result)
		yield put({
			type:'change',
			payload:{
				id:result.data._id,
				userName:result.data.userName,
				nickName:result.data.nickName,
				password:result.data.password,
				avatar:result.data.avatar
			}
		})
	},
	*getProductData(payload,{call,put}){
		const result=yield call(api.getProduct,payload.payload)
		console.log(payload.payload)
		console.log(result)
		yield put({
			type:'change',
			payload:{
				productlist:result.data.products,
				page:result.data.pages,
				totalCount:result.data.totalCount,
				current:payload.payload.page
			}
		})
	},
	*postAddProduct(payload,{call,put}){
		const result=yield call(api.postAddProduct,payload.payload)
		console.log(payload.payload)
		console.log(result)
		yield put({
			type:'change',
			payload:{
				mess:result.data._id
			}
		})
	},
	*postDeleteProduct(payload,{call,put}){
		const result=yield call(api.postDeleteProduct,payload.payload)
		console.log(payload.payload)
		console.log(result)
		yield put({
			type:'change',
			payload:{
				mess:result.data.code
			}
		})
	},
	*editProduct(payload,{call,put}){
		console.log(payload)
		let a=function(){
			return api.editProduct(payload.payload)
		}  
		const result=yield call(a,payload.payload)
		console.log(result)
	},
	*getProductDetail(payload,{call,put}){
		console.log(payload)
		const result=yield call(api.getProductDetail,payload.payload)
		console.log(result)
		yield put({
			type:'change',
			payload:{
				id:result.data._id,
				productName:result.data.name,
				productDesc:result.data.descriptions,
				productPrice:result.data.price,
				productQuantity:result.data.quantity,
				productIamge:result.data.coverImg,
				productCategory:result.data.productCategory,
				productOnSale:result.data.onSale
			}
		})
	},
	*getProductCategories(payload,{call,put}){
		const result=yield call(api.getProductCategories,payload.payload)
		console.log(payload.payload)
		console.log(result.data)
		yield put({
			type:'change',
			payload:{
				productlist:result.data.categories,
				page:result.data.pages,
				totalCount:result.data.totalCount,
				current:payload.payload.page
			}
		})
	},
	*addProductCategoriesInfo(payload,{call,put}){
		console.log(payload)
		const result=yield call(api.addProductCategoriesInfo,payload.payload)
		console.log(result)
		yield put({
			type:'change',
			payload:{
				mess:result.data._id
			}
		})
	},
	*deleteProductCategoriesInfo(payload,{call,put}){
		const result=yield call(api.deleteProductCategoriesInfo,payload.payload)
		console.log(payload.payload)
		console.log(result)
	},
	*editProductCategoriesInfo(payload,{call,put}){
		console.log(payload)
		let a=function(){
			return api.editProductCategoriesInfo(payload.payload)
		}  
		const result=yield call(a,payload.payload)
		console.log(result)
	},
	*getProductCategoriesInfo(payload,{call,put}){
		console.log(payload)
		const result=yield call(api.getProductCategoriesInfo,payload.payload)
		console.log(result)
		yield put({
			type:'change',
			payload:{
				id:result.data._id,
				productName:result.data.name,
				productDesc:result.data.descriptions,
				productIamge:result.data.coverImg,
				productCreatedAt:result.data.createdAt
			}
		})
	},
	
	*getOrderList(payload,{call,put}){
		const result=yield call(api.getOrderList,payload.payload)
		console.log(payload)
		console.log(result.data.orders)
		yield put({
			type:'change',
			payload:{
				productlist:result.data.orders,
				page:result.data.pages,
				totalCount:result.data.totalCount,
				current:payload.payload.page
			}
		})
	},
	*deleteOrder(payload,{call,put}){
		const result=yield call(api.deleteOrder,payload.payload)
		console.log(payload.payload)
		console.log(result)
	},
	*getOrderInfo(payload,{call,put}){
		console.log(payload)
		const result=yield call(api.getOrderInfo,payload.payload)
		console.log(result)
		yield put({
			type:'change',
			payload:{
				id:result.data._id,
				orderNo:result.data.no,
				orderPrice:result.data.price,
				userId:result.data.user,
				orderCreatedAt:result.data.createdAt,
				orderIsPayed:result.data.isPayed
			}
		})
	},
	*editOrder(payload,{call,put}){
		console.log(payload)
		let a=function(){
			return api.editOrder(payload.payload)
		}  
		const result=yield call(a,payload.payload)
		console.log(result)
	},
  }
};
